package com.zhenta.controller;

import com.zhentao.mapper.AppointmentMapper;
import com.zhentao.pojo.AppointmentVO;
import com.zhentao.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 数据库测试控制器
 */
@RestController
@RequestMapping("/test")
@CrossOrigin(origins = "*")
public class DatabaseTestController {

    @Autowired
    private AppointmentMapper appointmentMapper;

    /**
     * 测试数据库连接
     */
    @GetMapping("/database")
    public Result<String> testDatabase() {
        try {
            // 尝试查询数据库
            List<AppointmentVO> result = appointmentMapper.selectAppointmentList();
            return Result.success("数据库连接成功，查询到 " + result.size() + " 条记录");
        } catch (Exception e) {
            return Result.error("数据库连接失败：" + e.getMessage());
        }
    }

    /**
     * 获取数据库中的实际数据
     */
    @GetMapping("/data")
    public Result<List<AppointmentVO>> getDatabaseData() {
        try {
            List<AppointmentVO> data = appointmentMapper.selectAppointmentList();
            return Result.success(data);
        } catch (Exception e) {
            return Result.error("获取数据失败：" + e.getMessage());
        }
    }

    /**
     * 获取数据库表结构信息
     */
    @GetMapping("/table-info")
    public Result<String> getTableInfo() {
        try {
            return Result.success("t_register表结构：\n" +
                    "- gid: 主键ID\n" +
                    "- uid: 用户ID\n" +
                    "- username: 用户名\n" +
                    "- number: 就诊卡号\n" +
                    "- id_number: 身份证号\n" +
                    "- phone: 手机号\n" +
                    "- dept_id: 科室ID\n" +
                    "- type: 科室类型\n" +
                    "- cost: 挂号费\n" +
                    "- status: 挂号状态\n" +
                    "- by_data: 就诊日期\n" +
                    "- by_time: 就诊时间\n" +
                    "- yy_time: 预约时间\n" +
                    "- hospitalname: 医院名称\n" +
                    "- hospitaladdress: 医院地址\n" +
                    "- doctor_id: 医生编号");
        } catch (Exception e) {
            return Result.error("获取表结构失败：" + e.getMessage());
        }
    }
} 